<?php
class VES_VendorsProduct_Adminhtml_Vendors_Catalog_ProductController extends Mage_Adminhtml_Controller_Action
{
    protected function _isAllowed()
    {
        return true;
    }
	
	protected function _initAction() {
		$this->loadLayout()
			->_setActiveMenu('vendors/vendors')
			->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
		
		return $this;
	}   
 
	public function pendingAction() {
		$this->_initAction()
			->renderLayout();
	}
	
	/**
     * Product grid for AJAX request
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    
	public function massApproveAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        $Ids  = array();
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
/* you 限制三审(翻译中)产品 2016-07-21 */
                        if($this->isInTranslate($productId)){continue;}
                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        $product->setData('approval',VES_VendorsProduct_Model_Source_Approval::STATUS_APPROVED)
                                ->setData('status','1')
                                ->save();
                        Mage::helper('vendorsproduct')->sendProductReviewdNotificationEmail($product,VES_VendorsProduct_Model_Source_Approval::STATUS_APPROVED);
                        $Ids[]=$productId;
                        
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been approved.', count($Ids))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/pending');
    }
    
	public function massRejectAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        $Ids  = array();
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
/* you 限制三审(翻译中)产品 2016-07-21 */
                        if($this->isInTranslate($productId)){continue;}

                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        $product->setData('approval',VES_VendorsProduct_Model_Source_Approval::STATUS_UNAPPROVED)->save();
                        Mage::helper('vendorsproduct')->sendProductReviewdNotificationEmail($product,VES_VendorsProduct_Model_Source_Approval::STATUS_UNAPPROVED);
                        $Ids[]=$productId;
                        
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been rejected.', count($Ids))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/pending');
    }
	
	public function mass1stapproveAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        $Ids  = array();
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
/* you 限制三审(翻译中)产品 2016-07-21 */
                        if($this->isInTranslate($productId)){continue;}
                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        $product->setData('approval',VES_VendorsProduct_Model_Source_Approval::STATUS_1STAPPROVED)
								->setData('status','2')
								->save();
                        $Ids[]=$productId;
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been 1stapproval.', count($Ids))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/pending');
    }
	
	public function mass2ndapproveAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        $Ids  = array();
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
/* you 限制三审(翻译中)产品 2016-07-21 */
                        if($this->isInTranslate($productId)){continue;}
                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        $product->setData('approval',VES_VendorsProduct_Model_Source_Approval::STATUS_2NDAPPROVED)
								->setData('status','2')
								->save();
                        $Ids[]=$productId;
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been 2ndapproval.', count($Ids))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/pending');
    }
	
	public function mass3rdapproveAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        $product->setData('approval',VES_VendorsProduct_Model_Source_Approval::STATUS_3RDAPPROVED)
								->setData('status','2')
								->save();
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been 3rdapproval.', count($productIds))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/pending');
    }
	
	
	public function massDeleteAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
        } else {
            if (!empty($productIds)) {
                try {
                    foreach ($productIds as $productId) {
                        $product = Mage::getSingleton('catalog/product')->load($productId);
                        Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product));
                        $product->delete();
                    }
                    $this->_getSession()->addSuccess(
                        $this->__('Total of %d record(s) have been deleted.', count($productIds))
                    );
                } catch (Exception $e) {
                    $this->_getSession()->addError($e->getMessage());
                }
            }
        }
        $this->_redirect('*/*/index');
    }

/* you 是否是三审(翻译中)产品 2016-07-21 */
    protected function isInTranslate($productId){
        $approval =  Mage::getModel('catalog/product')->load($productId)->getApproval();//商品状态
        if($approval == VES_VendorsProduct_Model_Source_Approval::STATUS_3RDAPPROVED){
            return true;
        }else{
            return false;
        }
    }
/* you 是否是三审(翻译中)产品 2016-07-21 */
}